<?php
include_once 'MY/extra_sql.php';
class GroupController extends Zend_Controller_Action{
	function init(){
		if($_SESSION['user']==""){
			echo "<script>window.location='../user/login';</script>"; exit;
		}
		$this->registry = Zend_Registry::getInstance();
		$this->view = $this->registry['view'];
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$this->view->template = "system";
		$this->view->type = "group";
		$this->view->conf = new Zend_Config(require './application/configs/config.php');
	}

	public function indexAction(){

	}

       public function listAction(){
            $filter=new Zend_Filter_StripTags();
            $group = new Group();
            $this->view->groups = $group->getAllGroups();


            echo $this->view->render('group/list.html');
     }
     
     public function addAction(){
          $filter=new Zend_Filter_StripTags();
          $group = new Group();
          if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
             $name=$filter->filter(($this->_request->getPost('name')));
             $model = $_POST['model'];
             $models = implode(',', $model);

             $data = array(
                 'name'=>$name,
                 'models'=>$models
             );

             $group_id = $group->insert($data);
             
             if($group_id){
                 echo "<script>window.location='list'</script>";
             }else{
                 echo "<script>alert('Failure!');window.location='list'</script>";
             }
          }
           echo $this->view->render('group/add.html');
     }

     public  function editAction(){
          $filter=new Zend_Filter_StripTags();
          $group = new Group();
          $group_id=$filter->filter(($this->_request->getParam('group_id')));
          if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
             $name=$filter->filter(($this->_request->getPost('name')));
             $group_id=$filter->filter(($this->_request->getPost('group_id')));
             $model = $_POST['model'];
             $models = implode(',', $model);

             $data = array(
                 'name'=>$name,
                 'models'=>$models
             );

             $group_id = $group->updategroupByid($data, $group_id);

             if($group_id){
                 echo "<script>window.location='list'</script>";
             }else{
                 echo "<script>alert('Failure!');window.location='list'</script>";
             }
          }else{
              $this->view->group = $group->getgroupByid($group_id);
              

          }
           echo $this->view->render('group/edit.html');
     }

    public function deleteAction(){
        $filter=new Zend_Filter_StripTags();
        $group = new Group();
        $group_id=$filter->filter(($this->_request->getParam('group_id')));

        $query = $group->delgroupByid($group_id);
        if($query){
                 echo "<script>window.location='list'</script>";
             }else{
                 echo "<script>alert('Failure!');window.location='list'</script>";
             }
    }
     
}
?>
